package com.lyshop.guiyingshanding.activitys;


import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lyshop.guiyingshanding.R;
import com.lyshop.guiyingshanding.base.BaseActivity;
import com.lyshop.guiyingshanding.bean.UserInfoBean;
import com.lyshop.guiyingshanding.utils.DataUtils;
import com.lyshop.guiyingshanding.utils.RouteUtil;
import com.lyshop.guiyingshanding.utils.ToastUtil;
import com.lyshop.guiyingshanding.view.CertificateModule;

public class SettingActivity extends BaseActivity implements View.OnClickListener {
    private Button bt_log_out;
    private View rl_nickname;
    private View rl_user_mobile;
    private View rl_update_password;
    private View rl_cancel_user;
    private View rl_header;
    private RelativeLayout rl_pay_password;
    private ImageView iv_header;
    private View rl_open_follow;
    private TextView tv_nick_name;
    private TextView tv_user_mobile;
    private TextView tv_version;
    private CheckBox cb_open_follow;

    private View rl_certificate;
    private TextView tv_cert_sign;
    private TextView tv_cert_name;
    private TextView tv_go_cert;
    private boolean mOpenPush;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mOpenPush = DataUtils.getBooleanSPByTag(mContext, "SP_OPEN_PUSH");
        changeOpenPush();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (DataUtils.isLogin(this)) {
            UserInfoBean bean = DataUtils.getUserInfo(mContext);
            String mobile = DataUtils.getMobile(this);
            Glide.with(mContext).load(bean.header).into(iv_header);
            tv_user_mobile.setText(CertificateModule.getMobileHide(mobile));
            tv_nick_name.setText(bean.nickname);
        } else {
            rl_nickname.setVisibility(View.GONE);
            rl_user_mobile.setVisibility(View.GONE);
            rl_update_password.setVisibility(View.GONE);
            bt_log_out.setVisibility(View.GONE);
            rl_cancel_user.setVisibility(View.GONE);
            rl_certificate.setVisibility(View.GONE);
            rl_header.setVisibility(View.GONE);
        }
        tv_version.setText(getVersion() + " (" + DataUtils.getClientId(mContext) + ")");


        UserInfoBean accountBean = DataUtils.getUserInfo(mContext);
        if (accountBean.auth_flag == 1&&accountBean.bank_flag == 1) {
            if(!TextUtils.isEmpty(accountBean.certname)){
                tv_cert_name.setText(CertificateModule.getNameHide(accountBean.certname));
                tv_cert_sign.setVisibility(View.VISIBLE);
                tv_cert_name.setVisibility(View.VISIBLE);
                tv_go_cert.setVisibility(View.GONE);
            }
        } else {
            tv_go_cert.setVisibility(View.VISIBLE);
            tv_cert_sign.setVisibility(View.GONE);
            tv_cert_name.setVisibility(View.GONE);
        }
    }

    private void changeOpenPush() {
        if (mOpenPush) {
            DataUtils.setBooleanSPByTag(mContext,"SP_OPEN_PUSH",true);
            cb_open_follow.setChecked(true);
        } else {
            DataUtils.setBooleanSPByTag(mContext,"SP_OPEN_PUSH",false);
            cb_open_follow.setChecked(false);
        }
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_setting;
    }

    @Override
    protected void findViews() {
        rl_nickname = findViewById(R.id.rl_nickname);
        rl_open_follow = findViewById(R.id.rl_open_follow);
        rl_user_mobile = findViewById(R.id.rl_user_mobile);
        rl_cancel_user = findViewById(R.id.rl_cancel_user);
        rl_update_password = findViewById(R.id.rl_update_password);
        rl_header = findViewById(R.id.rl_header);
        cb_open_follow = findViewById(R.id.cb_open_follow);
        tv_nick_name = findViewById(R.id.tv_nick_name);
        tv_user_mobile = findViewById(R.id.tv_user_mobile);
        tv_version = findViewById(R.id.tv_version);
        bt_log_out = findViewById(R.id.bt_log_out);

        rl_certificate = findViewById(R.id.rl_certificate);
        tv_cert_sign = findViewById(R.id.tv_cert_sign);
        tv_cert_name = findViewById(R.id.tv_cert_name);
        tv_go_cert = findViewById(R.id.tv_go_cert);
        iv_header = findViewById(R.id.iv_header);
        rl_pay_password = findViewById(R.id.rl_pay_password);
    }

    @Override
    protected void setListeners() {
        bt_log_out.setOnClickListener(this);
        rl_update_password.setOnClickListener(this);
        cb_open_follow.setOnClickListener(this);
        rl_cancel_user.setOnClickListener(this);
        tv_go_cert.setOnClickListener(this);
        rl_nickname.setOnClickListener(this);
        rl_header.setOnClickListener(this);
        rl_pay_password.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_log_out:
                logout();
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        String token = getToken("20210511006", "B8B45BEA7F05AB938E9DB5BC2FBB03351E16C72F18D69F7DCE0B544EFE9E9D74",
//                                "4715313258a87bbfa39b090981664d9d6a5dadb83d101cc775a05fe9ee42da5f");
//                        Log.e("art",token);
//                    }
//                }).start();
                finish();
                break;
            case R.id.rl_nickname:
                RouteUtil.gotoActivity(this, EditNickNameActivity.class, null);
                finish();
                break;
            case R.id.rl_update_password:
//                Bundle bundle = new Bundle();
//                bundle.putString("title", "修改密码");
//                RouteUtil.gotoActivity(this, UpdatePasswordActivity.class, bundle);
                RouteUtil.toFindBackPwdActivity(this);
                break;
            case R.id.cb_open_follow:
                mOpenPush = !mOpenPush;
                changeOpenPush();
                break;
            case R.id.rl_cancel_user:
                RouteUtil.gotoActivity(this, CancelAccountActivity.class, null);
                break;
            case R.id.tv_go_cert:
//                CertificateModule.getInstance(mContext).isCertificate(null);
                if (CertificateModule.getInstance(mContext).isCertificate(null)) {
                    ToastUtil.showToast(mContext,"已完成认证");
                }
                break;
            case R.id.rl_header:
                RouteUtil.gotoActivity(this, HeaderSelectActivity.class, null);
                finish();
                break;
            case R.id.rl_pay_password:
                RouteUtil.tosetPayPwdActivity(this);
                break;
        }
    }


//    public String getToken(String clientId,String secret,String code) {
//        try{
//            OkHttpClient client = new OkHttpClient().newBuilder().build();
//            Request request = new Request.Builder().url("https://marketing-api.vivo.com.cn/openapi/v1/oauth2/token?client_id="+clientId+"&client_secret="+secret+"&grant_type=code&code="+code)
//                    .method("GET", null)
//                    .build();
//            Response response = client.newCall(request).execute();
//            return response.body().string();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return "";
//    }

    private void logout() {
        DataUtils.clearUserInfo(this);
//        sendBroadcast(new Intent(MyApplication.GO_TO_LOGOUT));
//        mAppAction.getAccessToken(new ActionCallbackListener<Void>() {
//            @Override
//            public void onSuccess(Void data) {
//                backActivity();
//            }
//
//            @Override
//            public void onFailure(String errorCode, String message) {
//                backActivity();
//            }
//        });
    }

    private String getVersion() {
        try {
            PackageManager manager = getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "Fail";
        }
    }
}
